package me.zhengjie.modules.apijson.rest;

import apijson.NotNull;
import apijson.RequestMethod;
import apijson.framework.APIJSONController;
import apijson.framework.APIJSONCreator;
import apijson.framework.APIJSONParser;
import apijson.orm.Parser;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.rest.AnonymousPostMapping;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

import static apijson.RequestMethod.*;

@RestController
@RequestMapping("")
@Api(tags="武士刀接口")
@Slf4j
public class KatanaController {
    private static final String TAG = "KatanaController";
    //通用接口，非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @NotNull
    public static APIJSONCreator APIJSON_CREATOR;


    public Parser<Long> newParser(HttpSession session, RequestMethod method) {
        Parser<Long> parser = APIJSON_CREATOR.createParser();
        parser.setMethod(method);
        if (parser instanceof APIJSONParser) {
            ((APIJSONParser) parser).setSession(session);
        }
        return parser;
    }

    private String parse(String request, HttpSession session, RequestMethod method) {
        return newParser(session, method).parse(request);
    }


    /**获取
     * @param request 只用String，避免encode后未decode
     * @param session
     * @return
     * @see {@link RequestMethod#GET}
     */
    @AnonymousPostMapping(value = "get")
    public JSONObject get(@RequestBody String request, HttpSession session) {
        return JSONUtil.parseObj(newParser(session, GET).parse(request));
    }

    /**计数
     * @param request 只用String，避免encode后未decode
     * @param session
     * @return
     * @see {@link RequestMethod#HEAD}
     */
    @AnonymousPostMapping("head")
    public JSONObject head(@RequestBody String request, HttpSession session) {
        return JSONUtil.parseObj(newParser(session, HEAD).parse(request));
    }

    /**限制性GET，request和response都非明文，浏览器看不到，用于对安全性要求高的GET请求
     * @param request 只用String，避免encode后未decode
     * @param session
     * @return
     * @see {@link RequestMethod#GETS}
     */
    @AnonymousPostMapping("gets")
    public JSONObject gets(@RequestBody String request, HttpSession session) {
        return JSONUtil.parseObj(newParser(session, GETS).parse(request));
    }

    /**限制性HEAD，request和response都非明文，浏览器看不到，用于对安全性要求高的HEAD请求
     * @param request 只用String，避免encode后未decode
     * @param session
     * @return
     * @see {@link RequestMethod#HEADS}
     */
    @AnonymousPostMapping("heads")
    public JSONObject heads(@RequestBody String request, HttpSession session) {
        return JSONUtil.parseObj(newParser(session, HEADS).parse(request));
    }

    /**新增
     * @param request 只用String，避免encode后未decode
     * @param session
     * @return
     * @see {@link RequestMethod#POST}
     */
    @AnonymousPostMapping("post")
    public JSONObject post(@RequestBody String request, HttpSession session) {
        return JSONUtil.parseObj(newParser(session, POST).parse(request));
    }

    /**修改
     * @param request 只用String，避免encode后未decode
     * @param session
     * @return
     * @see {@link RequestMethod#PUT}
     */
    @AnonymousPostMapping("put")
    public JSONObject put(@RequestBody String request, HttpSession session) {
        return JSONUtil.parseObj(newParser(session, PUT).parse(request));
    }

    /**删除
     * @param request 只用String，避免encode后未decode
     * @param session
     * @return
     * @see {@link RequestMethod#DELETE}
     */
    @AnonymousPostMapping("delete")
    public JSONObject delete(@RequestBody String request, HttpSession session) {
        return JSONUtil.parseObj(newParser(session, DELETE).parse(request));
    }
}
